package com.qctpos.crm.global.Interceptor;

import com.auth0.jwt.exceptions.JWTVerificationException;
import com.qctpos.crm.global.annotation.PassToken;
import com.qctpos.crm.global.annotation.UserLoginToken;
import com.qctpos.crm.global.utils.JwtTokenUtil;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;

public class AuthenticationInterceptor implements HandlerInterceptor {


    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object object) throws Exception {

        // 如果不是映射到方法直接通过
        if(!(object instanceof HandlerMethod)){
            return true;
        }
        HandlerMethod handlerMethod=(HandlerMethod)object;
        Method method=handlerMethod.getMethod();

        boolean isClassPassTokenAnnotation = handlerMethod.getBeanType().isAnnotationPresent(PassToken.class);
        boolean isMethodPassTokenAnnotation = method.isAnnotationPresent(PassToken.class);
        boolean isClassUserLoginTokenAnnotation = handlerMethod.getBeanType().isAnnotationPresent(UserLoginToken.class);
        boolean isMethodUserLoginTokenAnnotation = method.isAnnotationPresent(UserLoginToken.class);
        //检查是否有passtoken注释，有则跳过认证
        if (isMethodPassTokenAnnotation || (isClassPassTokenAnnotation&&!isMethodUserLoginTokenAnnotation)) {
            PassToken passToken =isMethodPassTokenAnnotation? method.getAnnotation(PassToken.class):handlerMethod.getBeanType().getAnnotation(PassToken.class);
            if (passToken.required()) {
                return true;
            }
        }
        //检查有没有需要用户权限的注解
        if (isClassUserLoginTokenAnnotation||isMethodUserLoginTokenAnnotation) {
            UserLoginToken userLoginToken =isMethodUserLoginTokenAnnotation ? method.getAnnotation(UserLoginToken.class) : handlerMethod.getBeanType().getAnnotation(UserLoginToken.class) ;
            if (userLoginToken.required()) {
                String token = httpServletRequest.getHeader(JwtTokenUtil.TOKEN_HEADER);// 从 http 请求头中取出 token
                // 执行认证
                if (token == null) {
                    throw new JWTVerificationException("无token，请重新登录");
                }

                try {
                    JwtTokenUtil.decode(token);
                } catch (JWTVerificationException e) {
                    throw new JWTVerificationException("token无效");
                }
                return true;
            }
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest,
                           HttpServletResponse httpServletResponse,
                           Object o, ModelAndView modelAndView) throws Exception {

    }
    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest,
                                HttpServletResponse httpServletResponse,
                                Object o, Exception e) throws Exception {
    }
}